<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/25
 * Time: 12:31
 */

namespace app\index\controller;

use app\common\controller\HomeBase;
use app\common\model\Attachment as AttachmentModel;
use app\common\model\BoolStatus;
use app\common\model\Category as CategoryModel;
use base\Url;
use think\Db;
use app\common\model\FavoritesItem;

class Lists extends HomeBase
{
    public function index()
    {
        $cid = $this->request->param('id', 0, 'int');

        $categoryModel = new CategoryModel();
        $category      = $categoryModel->where('status', '=', BoolStatus::OPEN)->find($cid);
        if (!$category) {
            abort(404);
        }

        $list = Db::view('article')
            ->view('article_detail', 'seo_desc', 'article.id=article_detail.id', 'left')
            ->view('attachment', 'attachment as img', 'article.id=attachment.tid and attachment.type="'.AttachmentModel::ARTICLE.'"', 'left')
            ->view('user', 'name as author', 'article.uid=user.id', 'left')
            ->where('article.status', '=', BoolStatus::OPEN)
            ->where('article.cid', '=', $cid)
            ->order('article.id', 'desc')
            ->paginate($this->pageSize);

        // 获取分页显示
        $page = $list->render();
        $listData = [];
        foreach ($list->getCollection() as $_item) {
            $_item['img'] = $_item['img'] ? Url::getRemoteImgSrc($_item['img']) : '';
            $listData[] = $_item;
        }

        // 模板变量赋值
        $this->assign('list', $listData);
        $this->assign('page', $page);

        $recommendGoodsList = (new FavoritesItem())->getRandRecommendData(4);
        $this->assign('recommendGoodsList', $recommendGoodsList);

        $this->assign('title', $category->name);
        $this->setSEOInfo($category->name, $category->seo_keywords, $category->seo_desc);
        return $this->fetch('lists/index');
    }

    /**
     * 空操作
     * @param $name
     * @return mixed
     */
    public function _empty($name)
    {
        $this->assign('title', $name);
        return $this->fetch('lists/index');
    }
}